//
//  EpointWebInfoDetailViewController.m
//  EpointFrame5
//
//  Created by 李亚春 on 14-1-21.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "EpointWebInfoDetailViewController.h"
#import "EpointWebInfoSingleRecordModel.h"
#import "EpointStringUtil.h"
#import "EpointWebInfoFeedBackModel.h"
#import "EpointWebInfoDetailAttachModel.h"
#import "EpointAttachOpenViewController.h"
#import "EpointBizTools.h"


@interface EpointWebInfoDetailViewController ()
{
    UIWebView *wv;
    UITabBar *tb;
    //正文
    UITabBarItem *zbZW;
    //附件
    UITabBarItem *zbFJ;
    //反馈
    UITabBarItem *zbFk;
    NSArray *tabItems;
    
    UITableView *tvFk;
    NSMutableArray *feedBackArray;
    
    UITableView *tvFj;
    UIButton *btAddFj;
    EpointWebInfoSingleRecordModel *infoModel;
    
    NSString *infoGuid;
    
    NSString *disContent;
}
@end

@implementation EpointWebInfoDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    infoGuid = [self.passParam objectForKey:@"InfoGuid"];
    
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addFK)];
    [self setRightBarButtonItemWithTitle:@"反馈" action:@selector(addFK)];
    
    CGRect rect = CGRectMake(0, 0, self.getFullContentFrame.size.width, self.getFullContentFrame.size.height);
    wv = [[UIWebView alloc]initWithFrame:rect];
    wv.backgroundColor = [UIColor whiteColor];
    wv.opaque = NO;
//    wv.scalesPageToFit = YES;
    
    [self.view addSubview:wv];
    
    tb = [[UITabBar alloc]initWithFrame:CGRectMake(0, self.getFullContentFrame.size.height-49, AppWidth, 49)];
    tb.delegate = self;
    zbZW = [[UITabBarItem alloc]initWithTitle:@"内容" image:[UIImage imageNamed:@"img_zw"] tag:0];
    
    zbFk = [[UITabBarItem alloc]initWithTitle:@"反馈" image:[UIImage imageNamed:@"img_fk"] tag:2];
    
    tabItems = [[NSArray alloc]initWithObjects:zbZW,zbFk, nil];
//    [tb setItems:tabItems];
    
    [tb setSelectedItem:zbZW];
    
    
    
    tvFk = [[UITableView alloc]initWithFrame:rect];
    tvFk.hidden = YES;
    tvFk.dataSource = self;
    tvFk.delegate = self;
    tvFk.tag = 102;
//    tvFk.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tvFk];

    tvFj = [[UITableView alloc]initWithFrame:rect];
    tvFj.hidden = YES;
    tvFj.dataSource = self;
    tvFj.delegate = self;
    tvFj.tag = 101;
    [self.view addSubview:tvFj];
    
    [self getData];
}

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if (item.tag==0) {
        wv.hidden = NO;
        tvFk.hidden = YES;
        tvFj.hidden = YES;
    }
    else if (item.tag==1) {
        wv.hidden = YES;
        tvFj.hidden = NO;
        tvFk.hidden = YES;
    }
    else if (item.tag==2) {
        
        UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addFK)];
        self.navigationItem.rightBarButtonItem = addButton;
        wv.hidden = YES;
        tvFj.hidden = YES;
        tvFk.hidden = NO;
    }
}

- (NSString *)createHTMLTableWithArray:(NSArray *)array {
    
    NSMutableString *feedHTML = [[NSMutableString alloc]initWithString:@""];
    if (array.count>0) {
        [feedHTML appendString:@"<br/><br/><TABLE style=\"BORDER-LEFT-WIDTH: 0px; BORDER-COLLAPSE: collapse; BORDER-RIGHT-WIDTH: 0px;\" cellSpacing=0 cellPadding=0 align=center border=0 width='100%'>"];
        [feedHTML appendString:@"<tr>"];
        [feedHTML appendString:@"<TD style=\"BORDER-RIGHT: medium none; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none;background-color: #33A1C9;text-align: center;\" height=40>"];
        [feedHTML appendString:[NSString stringWithFormat:@"<font color=white><strong>信 息 反 馈</strong></font>"]];
        [feedHTML appendString:@"</td>"];
        [feedHTML appendString:@"</tr>"];
        for (int i=0; i<array.count; i++) {
            EpointWebInfoFeedBackModel *m = [array objectAtIndex:i];
            if (i%2 !=0) {
                [feedHTML appendString:@"<tr>"];
                [feedHTML appendString:@"<TD style=\"BORDER-RIGHT: medium none; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none;background-color: #DCDCDC;\" height=40>"];
                [feedHTML appendString:[NSString stringWithFormat:@"<font color='#3D59AB'>%@&nbsp;&nbsp;%@</font>",m.DispName,m.BackTime]];
                [feedHTML appendString:@"</td>"];
                [feedHTML appendString:@"</tr>"];
                [feedHTML appendString:@"<tr>"];
                [feedHTML appendString:@"<td style=\"BORDER-RIGHT: medium none; BORDER-LEFT: medium none; BORDER-TOP: medium none;background-color: #DCDCDC;\" height=40><font color='#0B1746'>"];
                [feedHTML appendString:m.FeedBackContent];
                [feedHTML appendString:@"</font></td>"];
                [feedHTML appendString:@"</tr>"];
            }else{
                [feedHTML appendString:@"<tr>"];
                [feedHTML appendString:@"<TD style=\"BORDER-RIGHT: medium none; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none;background-color: #FFFAF0;\" height=40>"];
                [feedHTML appendString:[NSString stringWithFormat:@"<font color='#5E2612'>%@&nbsp;&nbsp;%@</font>",m.DispName,m.BackTime]];
                [feedHTML appendString:@"</td>"];
                [feedHTML appendString:@"</tr>"];
                [feedHTML appendString:@"<tr>"];
                [feedHTML appendString:@"<td style=\"BORDER-RIGHT: medium none; BORDER-LEFT: medium none; BORDER-TOP: medium none;background-color: #FFFAF0;\" height=40><font color='#734A12'>"];
                [feedHTML appendString:m.FeedBackContent];
                [feedHTML appendString:@"</font></td>"];
                [feedHTML appendString:@"</tr>"];
            }
            
        }
        [feedHTML appendString:@"</table>"];
    }
    return feedHTML;
}

-(void)addFK
{
    [EpointInputViewController involeInputViewController:self vctitle:@"反馈" preContent:@"" handler:^(NSString *text){
        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
        [params setObject:infoGuid forKey:@"InfoGuid"];
        [params setObject:text forKey:@"FeedBackContent"];
        [self startTask:@"WebInfo_AddFeedBack" withParams:params withTaskId:3];
        
    }];
}

-(void)getData
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:infoGuid forKey:@"InfoGuid"];
    [params setObject:[self.passParam objectForKey:@"CategoryGuid"] forKey:@"CategoryGuid"];
    [self startTask:@"WebInfo_GetDetail_Task" withParams:params withTaskId:1];
}

-(void)getFKData
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:infoGuid forKey:@"InfoGuid"];
    [params setObject:[self.passParam objectForKey:@"CategoryGuid"] forKey:@"CategoryGuid"];
    [self startTask:@"EpointGetFeedBackListTask" withParams:params withTaskId:2];
}

-(void)refreshUIWithThreadGuid:(int)taskId andData:(NSObject *)obj
{
    //处理异常信息
    NSMutableArray *arr = (NSMutableArray*)obj;
    if (![[arr objectAtIndex:0]isEqualToString:@"1"]) {
        [self toastMessage:[arr objectAtIndex:1]];
        return;
    }
    if (taskId==1) {
        
        NSMutableArray *listData = [arr objectAtIndex:2];
        infoModel = [listData objectAtIndex:0];
        
        
        disContent = [NSString stringWithFormat:@"%@<div id='oschina_title'>%@</div><div id='oschina_outline'>%@ %@</div><hr />%@",HTMLSTYPE1,infoModel.title,infoModel.postUserName,[[infoModel.infoDate componentsSeparatedByString:@" "] objectAtIndex:0],infoModel.content];
        [wv loadHTMLString:disContent baseURL:nil];
        
        if (infoModel.AttachFiles.count>0) {
            zbFJ = [[UITabBarItem alloc]initWithTitle:@"附件" image:[UIImage imageNamed:@"img_fj2"] tag:1];
            zbFJ.badgeValue = [NSString stringWithFormat:@"%i",infoModel.AttachFiles.count];
            tabItems = [[NSArray alloc]initWithObjects:zbZW,zbFJ, nil];
            [tb setItems:tabItems];
            [self.view addSubview:tb];
            wv.frame =  CGRectMake(0, 0, self.getFullContentFrame.size.width, self.getFullContentFrame.size.height-49);
            [tvFj reloadData];
        }
        
        [self getFKData];
    }
    else if(taskId==2)
    {
        feedBackArray = [arr objectAtIndex:2];
        NSString *fkStr = [NSString stringWithFormat:@"%@%@",disContent,[self createHTMLTableWithArray:feedBackArray]];
        [wv loadHTMLString:fkStr baseURL:nil];
//
//        if (feedBackArray.count>0) {
//            zbFk.badgeValue = [NSString stringWithFormat:@"%i",feedBackArray.count];
//        }
//        [tvFk reloadData];
    }
    else if(taskId==3)
    {
        [self getFKData];
    }
}

#pragma 表格代理

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag==101) {
        return infoModel.AttachFiles.count;
    }
    else if(tableView.tag==102)
    {
        return feedBackArray.count;
    }
    else
    {
        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag==102) {
        static NSString *identifier = @"FKCELL";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell==nil) {
            cell = [[UITableViewCell alloc]init];
        }
        
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        EpointWebInfoFeedBackModel *m = [feedBackArray objectAtIndex:indexPath.row];
        
        UIImageView *ivHead = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"img_man2"]];
        ivHead.frame = CGRectMake(5, 7, 15, 15);
        [cell.contentView addSubview:ivHead];
        
        UILabel *lbDispName = [[UILabel alloc]init];
        lbDispName.text = m.DispName;
        lbDispName.frame = CGRectMake(30, 5, 165, 20);
        lbDispName.font = [UIFont systemFontOfSize:13];
        lbDispName.backgroundColor = [UIColor clearColor];
        
        UIColor *c = [UIColor colorWithRed:89/255.0 green:140/255.0 blue:193/255.0 alpha:1];
        lbDispName.textColor = c;
        [cell.contentView addSubview:lbDispName];
        
        UILabel *lbSendTime = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width-130, 5, 125, 20)];
        lbSendTime.font = [UIFont systemFontOfSize:13];
        lbSendTime.backgroundColor = [UIColor clearColor];
        lbSendTime.text = m.BackTime;
        lbSendTime.textAlignment = NSTextAlignmentRight;
        lbSendTime.textColor = [UIColor colorWithRed:143/255.0 green:143/255.0 blue:143/255.0 alpha:1];
        [cell.contentView addSubview:lbSendTime];
        
        UILabel *lbFeedContent = [EpointUIUtils getFeedBackLabel:m.FeedBackContent];
        [cell.contentView addSubview:lbFeedContent];
//        lbFeedContent.backgroundColor = [UIColor lightGrayColor];
//        lbFeedContent.textColor = [UIColor blackColor];
//        lbFeedContent.layer.cornerRadius = 5;
        return cell;
    }
    else if (tableView.tag==101)
    {
        static NSString *identifier = @"FJCELL";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell==nil) {
            cell = [[UITableViewCell alloc]init];
        }
        EpointWebInfoDetailAttachModel *am = [infoModel.AttachFiles objectAtIndex:indexPath.row];
        cell.textLabel.text = am.AttFileName;
        cell.textLabel.numberOfLines = 2;
        cell.textLabel.lineBreakMode = NSLineBreakByCharWrapping;
        return cell;
    }
    else{
        return nil;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag==102) {
        EpointWebInfoFeedBackModel *m = [feedBackArray objectAtIndex:indexPath.row];
        UILabel *lbFeedContent = [EpointUIUtils getFeedBackLabel:m.FeedBackContent];
        return 40+lbFeedContent.frame.size.height;
    }
    else if (tableView.tag==101)
    {
        return 50;
    }
    else
    {
        return 0;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag==102) {

    }
    else if (tableView.tag==101)
    {
        
        EpointWebInfoDetailAttachModel *am = [infoModel.AttachFiles objectAtIndex:indexPath.row];
        [EpointBizTools openAttachNavController:self url:am.DownLoadUrl fileName:am.AttFileName folderGuid:infoGuid];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillDisappear:(BOOL)animated
{
    [wv stopLoading];
}

@end
